package com.atguigu.guilimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);    //指定线程池的数量
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main.............start.......");

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "黑色 + 256g";
        }, executor);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品的图片信息");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello.jpg";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的介绍");
            return "华为";
        }, executor);

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureAttr, futureImg, futureDesc);
        anyOf.get();    //等待所有线程执行完
        System.out.println("main.........end......." + anyOf.get());
    }

//        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1线程：" + Thread.currentThread().getId());
//            int i = 10 / 5;
//            System.out.println("任务1线程结束");
//            return i;
//        }, executor);
//        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2线程：" + Thread.currentThread().getId());
//            try {
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("任务2线程结束");
//            return "hello";
//        }, executor);
//
//        CompletableFuture<String> future = future01.applyToEitherAsync(future02, res -> {
//            System.out.println("任务3开始...结果：" + res);
//            return res.toString() + " ==> 哈哈";
//        }, executor);
//        System.out.println("main.............end......." + future.get());
//    }

//        future01.acceptEitherAsync(future02, (res) -> {
//            System.out.println("任务3开始...结果：" + res);
//        }, executor);
//    }

//        future01.runAfterEitherAsync(future02, () -> {
//            System.out.println("任务3开始...");
//        }, executor);
//    }


//        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1线程：" + Thread.currentThread().getId());
//            int i = 10 / 4;
//            System.out.println("任务1结束");
//            return i;
//        }, executor);
//
//        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2线程：" + Thread.currentThread().getId());
//            System.out.println("任务2结束");
//            return "hello";
//        }, executor);
//
//        /**
//         * 能得到两个任务的参数，无返回结果
//         */
//        CompletableFuture<String> thenCombineAsync = future01.thenCombineAsync(future02, (f1, f2) -> {
//            System.out.println("任务3开始。。。之前的结果 f1: " + f1 + " ==> f2: " + f2);
//            return f1 + ":" + f2 + " -> haha";
//        }, executor);
//        System.out.println("main.............end......." + thenCombineAsync.get());
//    }


    /**
     * 能得到两个任务的参数，无返回结果
     */
//        future01.thenAcceptBothAsync(future02, (f1, f2)->{
//            System.out.println("任务3开始。。。之前的结果 f1: " + f1 + " ==> f2: " + f2);
//        }, executor);

    /**
     * 不能得到两个任务的参数，也无返回结果
     */
//        future01.runAfterBothAsync(future02, () -> {
//            System.out.println("任务3开始。。。");
//        }, executor);


    /**
     * 线程串行化
     * thenAcceptAsync能接收上一步返回结果，但无返回值
     */
//        CompletableFuture<Void> thenAcceptAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 5;
//            System.out.println("当前运行结果:" + i);
//            return i;
//        }, executor).thenAcceptAsync(res -> {
//            System.out.println("任务2启动了。。。。" + res);
//        }, executor);
//        System.out.println("main.............end......." + thenAcceptAsync.get());


    /**
     * 线程串行化
     * thenRun 不能获取得到上一步的执行结果
     */
//        CompletableFuture<Void> thenRunAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 5;
//            System.out.println("当前运行结果: " + i);
//            return i;
//        }, executor).thenRunAsync(() -> {
//            System.out.println("任务2启动了。。。。");
//        }, executor);
//        System.out.println("main.............end......." + thenRunAsync.get());

    /**
     * 线程串行化
     * thenApplyAsync能接收上一步的返回结果，也有返回值
     */
//        CompletableFuture<String> thenApplyAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 5;
//            System.out.println("当前运行结果:" + i);
//            return i;
//        }, executor).thenApplyAsync(res -> {
//            System.out.println("任务2启动了。。。。" + res);
//            return "hello" + res;
//        }, executor);
//        System.out.println("main.............end......." + thenApplyAsync.get());
//    }


//        /**
//         * 方法执行完成后的处理
//         */
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 5;
//            System.out.println("当前运行结果:" + i);
//            return i;
//        }, executor).handle((result, exception) -> {
//            if (result != null){
//                return result * 2;
//            }
//            if (exception != null){
//                return 0;
//            }
//            return 0;
//        });
//        Integer integer = future.get();
//        System.out.println("main.............end......." + integer);
//    }

//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("当前运行结果:" + i);
//        }, executor);

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("当前运行结果:" + i);
//            return i;
//        }, executor).whenComplete((result, exception) -> {
//            //虽然能得到异常信息，但是没法修改返回数据
//            System.out.println("异步任务完成了...结果是：" + result + ";异常是：" + exception);
//            //可以感知异常，同时返回默认值
//        }).exceptionally(throwable -> {
//            return 10;
//        });
//        Integer integer = future.get();
//        System.out.println("main.............end......." + integer);
//    }

//    public static void main2(String[] args) throws ExecutionException, InterruptedException {
//        System.out.println("main.............start.......");
//
//        //1、继承Thread
//        Thread01 thread01 = new Thread01();
//        thread01.start();    //启动线程
//
//        //2、实现Runnable
//        Runnable01 runnable01 = new Runnable01();
//        new Thread(runnable01).start();
//
//        //3、实现Callable接口 + FutureTask
//        FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
//        new Thread(futureTask).start();
//
//        //阻塞等待整个线程执行完成，获取返回结果
//        Integer integer = futureTask.get();
//
//        //4、线程池
//        service.execute(new Runnable01());
//        System.out.println("main.............end......." + integer);
//    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("当前运行结果:" + i);
        }
    }

    public static class Runnable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("当前运行结果:" + i);
        }
    }

    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("当前运行结果:" + i);
            return i;
        }
    }
}
